import numpy as np
import pandas as pd

def read(file_path = '附件6：P3-P4的行驶路径.xlsx'):
    M = np.load('map_data.npy')
    df = pd.read_excel(file_path)
    array = df[['编号','栅格x坐标', '栅格y坐标','车头朝向（单位：度）']].values.tolist()
    for i in array:
        i.append(M[i[1],i[2],1])
    return array
path_name="T3最佳路径.xlsx"# '附件6：P3-P4的行驶路径.xlsx' 以计算第二问
path=read(path_name)
bad=[]
dic1 = {
    0:   [[-1, 1], [0, 1], [1, 1]],
    45:  [[0, 1], [1, 1], [1, 0]],
    90:  [[1, 1], [1, 0], [1, -1]],
    135: [[1, 0], [1, -1], [0, -1]],
    180: [[1, -1], [0, -1], [-1, -1]],
    225: [[0, -1], [-1, -1], [-1, 0]],
    270: [[-1, -1], [-1, 0], [-1, 1]],
    315: [[-1, 0], [-1, 1], [0, 1]]
}
dic2 = {
    (0, (-1, 1)): [270, 315],(0, (0, 1)): [315, 0, 45],(0, (1, 1)): [45, 90],
(45, (0, 1)): [315, 0],(45, (1, 1)): [0, 45, 90],(45, (1, 0)): [90, 135],
(90, (1, 1)): [0, 45], (90, (1, 0)): [45, 90, 135], (90, (1, -1)): [135, 180],
(135, (1, 0)): [45, 90], (135, (1, -1)): [90, 135, 180], (135, (0, -1)): [180, 225],
(180, (1, -1)): [90, 135], (180, (0, -1)): [135, 180, 225], (180, (-1, -1)): [225, 270],
(225, (0, -1)): [135, 180], (225, (-1, -1)): [180, 225, 270], (225, (-1, 0)): [270, 315],
(270, (-1, -1)): [180, 225], (270, (-1, 0)): [225, 270, 315], (270, (-1, 1)): [315, 0],
(315, (-1, 0)): [225, 270], (315, (-1, 1)): [270, 315, 0], (315, (0, 1)): [0, 45],
}
for i in range(1,len(path)):
    x, y,theta = path[i][1], path[i][2], path[i][3]
    xp, yp,thetap = path[i-1][1], path[i-1][2], path[i-1][3]
    dP =[x-xp,y-yp]

    if(not dP in dic1[thetap]):
        bad.append(path[i-1][0]+'-'+path[i][0]+':非可通行栅格\n')
    else:
        if(not theta in dic2[(thetap,(dP[0],dP[1]))]):
            bad.append(path[i-1][0]+'-'+path[i][0]+':车头方向错误\n')

with open(path_name+"_ans.txt", 'w', encoding='utf-8') as file:
    for i in bad:
        file.write(i)
    